package pers.lt.exception;

/**
 * Created by liting on 2015/12/7.
 * 统一异常处理
 */

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

public class CommonExceptionHandler implements HandlerExceptionResolver {
    //日志
    private static Log logger = LogFactory.getLog(CommonExceptionHandler.class);

    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object
            handler, Exception ex) {
        Map<String, Object> model = new HashMap<String, Object>();
        model.put("ex", ex.toString());
        // 根据不同错误转向不同页面
        logger.error(ex.toString());
        return new ModelAndView("/error", model);
        //错误记录日志

        //return new ModelAndView("/error", model);
//        if (ex instanceof BusinessException) {
//            return new ModelAndView("error-business", model);
//        } else if (ex instanceof ParameterException) {
//            return new ModelAndView("error-parameter", model);
//        } else {
//            return new ModelAndView("error", model);
//        }
    }
}
